package com.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobaibai.common.IndexEnum;
import com.xiaobaibai.dao.IndexDetailDao;
import com.xiaobaibai.entity.IndexDetail;
import com.xiaobaibai.service.IIndexDetailService;
import com.xiaobaibai.units.IndexDetailListSortUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2020-10-16
 */
@Service
public class IndexDetailServiceImpl extends ServiceImpl<IndexDetailDao, IndexDetail> implements IIndexDetailService {

    @Cacheable(value = {"indexDetail"},key = "#root.methodName")
    @Override
    public Map<String, List<IndexDetail>> getIndexDetailMap() {

        QueryWrapper<IndexDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","name","type","image","url","sort");
//        queryWrapper.eq("status",1);
        List<IndexDetail> list = this.list(queryWrapper);

        Map<String,List<IndexDetail>> map=new HashMap<>();
        List<IndexDetail> lunbo=new ArrayList<>(5);
        List<IndexDetail> gongge=new ArrayList<>(10);
        List<IndexDetail> datu=new ArrayList<>(1);

        list.forEach(l->{
            if(l.getType().equals(IndexEnum.LUNBO.getCode())){
                lunbo.add(l);
            }else if(l.getType().equals(IndexEnum.GONGGE.getCode())){
                gongge.add(l);
            }else if(l.getType().equals(IndexEnum.DATU.getCode())){
                datu.add(l);
            }
        });

        sortList(lunbo);
        sortList(gongge);

        map.put(IndexEnum.LUNBO.getName(),lunbo);
        map.put(IndexEnum.GONGGE.getName(),gongge);
        map.put(IndexEnum.DATU.getName(),datu);

        return map;
    }

    private IndexDetailListSortUnit sortUnit=new IndexDetailListSortUnit();

    private void sortList(List<IndexDetail> list){
        list.sort(sortUnit);
    }

}
